<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_laporan_invoice extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_lap_client_invoice_berlangganan_model', 'IspInvoice');
		//$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Laporan Invoice Berlangganan";
		viewByTemplate('isp_laporan_invoice/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspInvoice->GetJSON();
	}

	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->IspInvoice->GetJSON_detail(post('no_invoice'));
	}

	function getdata_detail()
	{

		$data['id']= $this->input->post('no_invoice');
		$this->load->model('isp_lap_client_invoice_berlangganan_model',$data);
		$this->secure_session();
		echo $this->IspPerakitanProduk->GetJSON_detail(post('no_invoice'));
	}

	function cetak(){
		$as_no_invoice	= get('no_invoice');
		$no_id = get('no_id');
		$kode_kontrak = get('kode_kontrak');
		//var_dump($no_id);

		//$no_invoice_new = substr($as_no_invoice, 0,22);
		//$this->IspInvoice->GetCetak(post($as_no_invoice));
		//$this->IspInvoice->GetCetak(post($no_id));

		$cek_data = $this->db->query("select * from isp_custom_invoice_detail where no_invoice = '".$as_no_invoice."'");

		// Load FPDF
		$this->load->helper('fpdf');

			if ($cek_data->num_rows() == 0) {
				// Set Data
				$data['data'] = $this->IspInvoice->GetCetak($as_no_invoice);
				$data['mode_cetak'] = "normal";
			} else {	
				// Set Data
				$data['data'] = $this->IspInvoice->GetCetak($as_no_invoice);
				$data['data_custom'] = $this->IspInvoice->GetCetakCustom($as_no_invoice);
				$data['mode_cetak'] = "custom";
			}

		$data['no_id'] = $no_id;
		$data['kode_kontrak'] =$kode_kontrak;
		$this->load->view('isp_laporan_invoice/cetak', $data);
			
	}

	function cetak_ext(){
		$as_no_invoice	= get('no_invoice');
		$no_id = get('no_id');

		$cek_data = $this->db->query("select * from isp_custom_invoice_detail where no_invoice = '".$as_no_invoice."'");

		// Load FPDF
		$this->load->helper('fpdf');

			if ($cek_data->num_rows() == 0) {
				// Set Data
				$data['data'] = $this->IspInvoice->GetCetak($as_no_invoice);
							
				$data['mode_cetak'] = "normal";
			} else {	
				// Set Data
				$data['data'] = $this->IspInvoice->GetCetak($as_no_invoice);
				$data['data_custom'] = $this->IspInvoice->GetCetakCustom($as_no_invoice);
							
				$data['mode_cetak'] = "custom";
			}
			
		$data['no_id'] = $no_id;
		$this->load->view('isp_laporan_invoice/cetak_tanpa_ttd', $data);
			
	}

	function custom()
	{
		// Display Form Create
		$data['title'] = "Custom Uraian Invoice";
		$data['data'] = $this->IspInvoice->GetSingleView(get('kode_kontrak'));
		$data['id_invoice'] = get('id_invoice');
		/*$query = $this->db->query("SELECT id_custom FROM (SELECT * FROM isp_custom_invoice ORDER BY id_custom DESC) WHERE rownum <=1");
	 					if ($query->row_array()) {
	 						$getquery = $query->row_array();
	 						$rowcount = $getquery['id_custom'];
	 						$autonum = $rowcount + 1;
	 					} else {
					$autonum = 1;
				}

		$data['id_custom_auto'] = $autonum;*/

		$query2 = $this->db->query("SELECT id_custom_detail FROM (SELECT * FROM isp_custom_invoice_detail ORDER BY id_custom_detail DESC) WHERE rownum <=1");
	 					if ($query2->row_array()) {
	 						$getquery2 = $query2->row_array();
	 						$rowcount2 = $getquery2['id_custom_detail'];
	 						$autonum2 = $rowcount2 + 1;
	 					} else {
					$autonum2 = 1;
				}
		
		$data['id_custom_detail_auto'] = $autonum2;

		$data['mode'] = "custom";

		$data['no_invoice'] = $this->IspInvoice->GetSingleView(get('no_invoice'));
		$cek_data = $this->db->query("select * from isp_custom_invoice_detail where no_invoice = '".$data['no_invoice']."'");
					if ($cek_data->num_rows() == 0) {
						$mode_detail = "add";
					} else {
						$mode_detail = "edit";
					}
		$data['mode_detail'] = $mode_detail;
		viewByTemplate('isp_laporan_invoice/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_kontrak") == '12') {
			$error->Add("kode_kontrak", "Kode Kontrak", $tpl_empty);
		
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{

				//Set Auto Increment
				/*$query = $this->db->query("SELECT id_custom FROM (SELECT * FROM isp_custom_invoice ORDER BY id_custom DESC) WHERE rownum <=1");
	 					if ($query->row_array()) {
	 						$getquery = $query->row_array();
	 						$rowcount = $getquery['id_custom'];
	 						$autonum = $rowcount + 1;
	 					} else {
					$autonum = 1;
				}*/

				/*$data	=  array(
					'id_custom' => $this->input->post('id_custom'),
					'kode_kontrak' => $this->input->post('kode_kontrak'),
					'no_invoice' => $this->input->post('no_invoice'),
					);
				
				if (post("mode") == "custom")
				{
					$cek_data = $this->db->query("select * from isp_custom_invoice where id_custom = '".$data['id_custom']."'");
					if ($cek_data->num_rows() == 0) {//
						$this->IspInvoice->Add($data);
					} else {
						$this->IspInvoice->Update($data, array('id_custom'	=> $data['id_custom']));
					}
					$ls_response = "Data Tersimpan";
				}*/

				// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->db->delete('isp_custom_invoice_detail', array('id_custom_detail'=> $row['id_custom_detail']));
					$ls_response = "Data Tersimpan";
				}
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
						$query2 = $this->db->query("SELECT id_custom_detail FROM (SELECT * FROM isp_custom_invoice_detail ORDER BY id_custom_detail DESC) WHERE rownum <=1");
		 					if ($query2->row_array()) {
		 						$getquery2 = $query2->row_array();
		 						$rowcount2 = $getquery2['id_custom_detail'];
		 						$autonum2 = $rowcount2 + 1;
		 					} else {
						$autonum2 = 1;
					}

					$row = $dtl_sample2['inserted'][$i];
					$row_data['id_custom_detail'] = $autonum2;
					$row_data['harga'] = $row['harga'];
					$row_data['uraian'] = $row['uraian'];
					$row_data['kode_kontrak'] = $this->input->post('kode_kontrak');
					$row_data['no_invoice'] = $this->input->post('no_invoice');
					$row_data['id_invoice'] = $this->input->post('id_invoice');
					
					if (post("mode") == "custom"){
						$cek_data = $this->db->query("select * from isp_custom_invoice_detail where no_invoice = '".$row['no_invoice']."'");
						if ($cek_data->num_rows() == 0) {
							$row_data['no_invoice'] = $autonum;
						} else {
							$row_data['no_invoice'] = $this->input->post('no_invoice');
						}
						
					}					

					$this->db->insert('isp_custom_invoice_detail',$row_data);
					$ls_response = "Data Tersimpan";
				}
					
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];		
					$row_data['harga'] = $row['harga'];
					$row_data['uraian'] = $row['uraian'];
					$this->db->update('isp_custom_invoice_detail',$row_data, array('id_custom_detail'=> $row['id_custom_detail']));
					$ls_response = "Data Tersimpan ['Edited']";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$no_invoice 	= get('no_invoice');
		
		if ($no_invoice != "")
		{
			// Delete Single
			$arr_rule	= array('no_invoice' => $no_invoice);
			$ls_result = $this->IspInvoice->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('no_invoice' => $row['no_invoice']);
				$ls_result .= $this->IspInvoice->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>